package com.mj.listen1._01数组;

/**
 *
 */
public class ArrayList<E> implements List<E> {

    private int size;

    private E[] elements;

    private static final int DEFAULT_CAPACITY = 10;

    private static final int ELE_NOT_FOND = -1;

    public ArrayList() {
        this(DEFAULT_CAPACITY);
    }

    public ArrayList(int capacity) {
        if (capacity < DEFAULT_CAPACITY) {
            capacity = DEFAULT_CAPACITY;
        }
        elements = (E[]) new Object[capacity];
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element) != ELE_NOT_FOND;
    }

    @Override
    public void add(E element) {
        add(size, element);
    }

    @Override
    public E get(int index) {
        checkOutOfBounds(index);
        return elements[index];
    }

    @Override
    public E set(int index, E element) {
        checkOutOfBounds(index);
        E res = elements[index];
        elements[index] = element;
        return res;
    }

    @Override
    public void add(int index, E element) {
        checkOutOfBoundsAdd(index);
        if (size == elements.length) {
            // 扩容原来大小的1.5倍。(elements.length >> 1)：除以2
            resize((elements.length >> 1) + elements.length);
        }
        for (int i = size - 1; i >= index; i--) {
            // 依次往后移
            elements[i + 1] = elements[i];
        }
        size++;
        elements[index] = element;
    }

    @Override
    public E remove(int index) {
        checkOutOfBounds(index);
        E res = elements[index];
        for (int i = index + 1; i < size; i++) {
            elements[i - 1] = elements[i];
        }
        size--;
        elements[size] = null;
        // 数组缩容
        if (size == elements.length / 2) {
            resize(elements.length / 2);
        }
        return res;
    }

    @Override
    public int indexOf(E element) {
        // 可以存储null值
        if (null == element) {
            for (int i = 0; i < size; i++) {
                // 只要找到第一个为null的位置，就直接返回当前索引
                if (elements[i] == null) {
                    return i;
                }
            }
        }
        for (int i = 0; i < size; i++) {
            if (element.equals(elements[i])) {
                return i;
            }
        }
        return ELE_NOT_FOND;
    }

    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            // 节省内存开销
            elements[i] = null;
        }
        size = 0;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("size:").append(size).append("，[");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                sb.append("，");
            }
            sb.append(elements[i]);
        }
        sb.append("]");
        return sb.toString();
    }

    private void checkOutOfBounds(int index) {
        if (index < 0 || index >= size) {
            throwOutOfBoundsException(index);
        }
    }

    private void checkOutOfBoundsAdd(int index) {
        if (index < 0 || index > size) {
            throwOutOfBoundsException(index);
        }
    }

    private void throwOutOfBoundsException(int index) {
        throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
    }

    /**
     * 数组扩容
     */
    private void resize(int newCapacity) {
        E[] newElements = (E[]) new Object[newCapacity];
        // 把原数组元素放置到新数组
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        System.out.println(elements.length + "扩容到：" + newCapacity);
        // 指针指向新的数组
        elements = newElements;
    }
}
